//app.js
const util = require('utils/util.js')
App({
  onLaunch: function () {

  },
  registered:function(userInfo){
    var data = {
      nickName: userInfo.nickName,
      avatarUrl: userInfo.avatarUrl,
      gender: userInfo.gender, //性别 0：未知、1：男、2：女
      province: userInfo.province,
      city: userInfo.city,
      country: userInfo.country,
      language: userInfo.language
    };
    this.post('identity/registered', data).then((res) => {
      if (res.code == 200) {
        console.info("getSetting----200---", res)
        this.globalData.userInfo = res.entity;
        wx.setStorageSync('userInfo', res.entity);
      }
      else {
        console.info("getSetting---fail----" , res.message)
      }
    }).catch((errMsg) => {
      console.log(errMsg)
      wx.showToast({
        title: "网络错误",
        icon: 'loading',
        duration: 2000
      })
    })
  },
  /** 
 * 自定义post函数，返回Promise
 * +-------------------
 * @param {String}      url 接口网址
 * @param {arrayObject} data 要传的数组对象 例如: {name: 'aa', age: 22}
 * +-------------------
 * @return {Promise}    promise 返回promise供后续操作
 */
  // openid:'oze4K4_rwDuvnZv2qvWG3XPzaOK0',
  post:function (url, data,dataType,header) {
    var promise = new Promise((resolve, reject) => {
      //init
      var that = this;
      var postData = data;
      /*
      //自动添加签名字段到postData，makeSign(obj)是一个自定义的生成签名字符串的函数
      postData.signature = that.makeSign(postData);
      */
      //网络请求
      wx.request({
        url: util.getUrl() + url,
        data: postData,
        dataType: dataType,
        method: 'POST',
        header: {
          'content-type': header || 'application/x-www-form-urlencoded',
          'openid': that.globalData.openId,
        },
        success: function (res) {//服务器返回数据
          // json参数返回的res.data也是json
          if (typeof (res.data) == "string") { res.data=JSON.parse(res.data)}
          if (res.statusCode == 200) {//res.data 为 后台返回数据，格式为{"data":{...}, "info":"成功", "status":1}, 后台规定：如果status为1,既是正确结果。可以根据自己业务逻辑来设定判断条件
            resolve(res.data);
          } else {//返回错误提示信息
            reject(res.data);

          }
        },
        error: function (e) {
          reject('网络出错');
        }
      })
    });
    return promise;
  },
  /*
    var that = this;
    var data = {};
    wx.showLoading({
      title: '加载中...',
    })
    app.post('',data).then((res)=>{
      wx.hideLoading();
        if (res.code == 200) {
          wx.showToast({
            title: res.message,
            icon: 'success',
            duration: 2000
          })
        } else {
          console.log(res.message)
          wx.showToast({
            title: res.message,
            icon: 'loading',
            duration: 2000
        })
      }
    }).catch((errMsg)=>{
      console.log(errMsg);
      wx.hideLoading();
      wx.showToast({
        title: "网络错误",
        icon: 'loading',
        duration: 2000
      })
    })
  */
  // 上传图片
  fetchUptoken: function () {
    return new Promise(function (resolve, reject) {
      wx.request({
        url: util.getUrl() + 'order/token',
        data: {},
        success: function (res) {
          // success
          resolve(res)
        },
        fail: function () {
          // fail
        },
        complete: function () {
          // complete
        }
      })
    })
  },
  globalData: {
    userInfo: null,
    openId:null,
    order:null,
    session_meid:0,
    agentRate:100,
    islogin:false,
    codeUrl:null,
    swiperbox: {
      indicatorDots: true,
      autoplay: true,
      interval: 3000,
      duration: 500,
    },
    swiper: [
      { id: null, url: 'http://file.gcb360.com/1.jpg' },
      { id: null, url: 'http://file.gcb360.com/2.jpg' },
      { id: null, url: 'http://file.gcb360.com/3.jpg' },
      { id: null, url: 'http://file.gcb360.com/4.jpg' },
      { id: null, url: 'http://file.gcb360.com/5.jpg' }
    ],
  }
})